<?php 
// +----------------------------------------------------------------------
// | ThinkPHP                                                             
// +----------------------------------------------------------------------
// | Copyright (c) 2008 http://thinkphp.cn All rights reserved.      
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: liu21st <liu21st@gmail.com>                                  
// +----------------------------------------------------------------------
// $Id$

// 显示运行时间、数据库操作、缓存次数、内存使用信息
function showRunTime(&$content) {
	if(C('SHOW_RUN_TIME')) {
		// 显示运行时间
		$startTime	 =	 $GLOBALS['_viewStartTime'];
		$endTime = microtime(true);
		$total_run_time	=	number_format(($endTime - $GLOBALS['_beginTime']), 3);
		$showTime	=	'Process: '.$total_run_time.'s ';
		if(C('SHOW_ADV_TIME') && isset($GLOBALS['_viewStartTime'])) {
			// 显示详细运行时间
			$_load_time	=	number_format(($GLOBALS['_loadTime'] -$GLOBALS['_beginTime'] ), 3);
			$_init_time	=	number_format(($GLOBALS['_initTime'] -$GLOBALS['_loadTime'] ), 3);
			$_exec_time	=	number_format(($startTime  -$GLOBALS['_initTime'] ), 3);
			$_parse_time	=	number_format(($endTime - $startTime), 3);
			$showTime .= '( Load:'.$_load_time.'s Init:'.$_init_time.'s Exec:'.$_exec_time.'s Template:'.$_parse_time.'s )';
		}
		if(C('SHOW_DB_TIMES') && class_exists('Db',false) ) {
			// 显示数据库操作次数
			$db	=	Db::getInstance();
			$showTime .= ' | DB :'.$db->Q().' queries '.$db->W().' writes ';
		}
		if(C('SHOW_CACHE_TIMES') && class_exists('Cache',false)) {
			// 显示数据库操作次数
			$cache	=	Cache::getInstance();
			$showTime .= ' | Cache :'.$cache->Q().' gets '.$cache->W().' writes ';
		}
		if(MEMORY_LIMIT_ON && C('SHOW_USE_MEM')) {
			// 显示内存开销
			$startMem    =  $GLOBALS['_startUseMems'];
			$endMem     =  memory_get_usage();
			$showTime .= ' | UseMem:'. number_format(($endMem - $startMem)/1024).' kb';
		}
		echo	'<div  class="think_run_time">'.$showTime.'</div>';
	}
	return ;
}
Tag::add('view_end','showRunTime');
?>